package com.yunchang.springboot.service.aTest;

import com.yunchang.springboot.mapper.ATestMapper;
import com.yunchang.springboot.models.ATest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service

public class ATestServiceImpl implements ATestService {
    @Autowired
    ATestMapper aTestMapper;

    private static String ACCESS_KEY = "AKIAI7ISC53ENUSBNB2A";
    private static String SECRET_KEY = "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";
    private static final String APP_NAME = "AmazonJavascriptScratchpad";
    private static final String APP_VERSION = "1.0";
    private static final String SERVICE_URL = "https://mws.amazonservices.com";
    private static final String client = null;

    @Override
    //@Transactional(propagation = Propagation.REQUIRES_NEW)
    public void test() {
        ATest aTest1 = new ATest();
//        aTest1.setId(3);
        aTest1.setName("sjd");
        aTest1.setAge(223);
        aTest1.setSex("man213");
        ATest aTest2 = new ATest();
        aTest2.setId(6);
        aTest2.setName("sjd345");
        aTest2.setAge(2233);
        aTest2.setSex("man1");
        ATest aTest3 = new ATest();
//        aTest1.setId(3);
        aTest3.setName("sj32d");
        aTest3.setAge(67);
        aTest3.setSex("man4");
        List<ATest> aTests = new ArrayList<>();
        aTests.add(aTest1);
        //aTests.add(aTest2);
        aTests.add(aTest3);

        for (ATest aTest : aTests) {
            insert(aTest);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insert(ATest aTest){
        //Object savepoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            aTestMapper.insertData(aTest);
            //TransactionAspectSupport.currentTransactionStatus().releaseSavepoint(savepoint);
        } catch (Exception e) {
        //    TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savepoint);
            e.printStackTrace();
        }
    }


    @Override
    public void staticTest(String accessKey, String secretKey, String serviceURL) {
        if (client == null || !(ACCESS_KEY.equals(accessKey) && SECRET_KEY.equals(secretKey))) {
            System.out.println("can run this");
            ACCESS_KEY = accessKey;
            SECRET_KEY = secretKey;
        }
    }

    public static void main(String[] args) {
        // 用所有不同的数字创建一个正方形列表

        List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
        List<Integer> distinct = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
        System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, distinct);
    }
}
